## Create Figure A4

## Loading packages
library(tidyverse)

## List of district-elections Y-axis of plot
left_out = c("2-2009", "4-2009", "6-2009",
             "1-2011", "3-2011", "5-2011", "7-2011",
             "2-2013", "4-2013", "6-2013",
             "1-2014",
             "1-2015", "3-2015", "5-2015", "7-2015",
             "2-2017", "4-2017", "6-2017",
             "5-2019")

#######################
## Panel A of Figure A4
#######################
## These are pasted from Stata output
continuous_linear_est = c(.0049275, .0047427, .0040662, .0040613,
                          .0045962, .0062713, .0063615, .0052082,
                          .0044977, .006117, .0048047, .0047093,
                          .0027302, .0038869, .0045423, .0047972,
                          .0049002, .0047087, .004685)
continuous_sq_est = c(-.0000442, -.0000423, -.0000397, -.0000338,
                      -.0000404, -.000054, -.0000601, -.0000478,
                      -.0000413, -.0000536, -.000043, -.0000418,
                      -.0000255, -.0000328, -.0000402, -.0000431,
                      -.0000433, -.0000419, -.0000382)

continuous_linear_se = c(.0017216, .0017232, .0017292, .001654,
                         .0017131, .001936, .0017103, .0019147,
                         .0017192, .0018627, .0017258, .0017228,
                         .0016694, .0018716, .0018631, .0017647,
                         .0017048, .0017216, .0017871)
continuous_sq_se = c(.000016, .0000161, .000016, .0000153, .000016,
                     .0000175, .0000159, .0000185,
                     .000016, .000017, .0000161, .0000161, .0000155,
                     .0000173, .000017, .0000168, .0000159, .000016, .000017)

continuous_linear_dta = data.frame(left_out, continuous_linear_est, continuous_linear_se)
continuous_linear_dta$term = "linear"
colnames(continuous_linear_dta) <- c("left_out", "est", "se", "term")
continuous_sq_dta = data.frame(left_out, continuous_sq_est, continuous_sq_se)
continuous_sq_dta$term = "squared"
colnames(continuous_sq_dta) <- c("left_out", "est", "se", "term")

continuous_dta = rbind(continuous_linear_dta, continuous_sq_dta) 
continuous_dta$left_out = factor(continuous_dta$left_out, levels = c("2-2009", "4-2009", "6-2009",
                                                                     "1-2011", "3-2011", "5-2011", "7-2011",
                                                                     "2-2013", "4-2013", "6-2013",
                                                                     "1-2014",
                                                                     "1-2015", "3-2015", "5-2015", "7-2015",
                                                                     "2-2017", "4-2017", "6-2017",
                                                                     "5-2019"))

continuous_dta = continuous_dta %>%
  mutate(
    upper = est + 1.96 * se,
    lower = est - 1.96 * se)

## Plot
figureA4a = ggplot(continuous_dta, aes(x = as.numeric(est), 
                                                      y = left_out, 
                                                      xmin=lower, 
                                                      xmax=upper,
                                                      color = factor(term), group = factor(term))) +
  geom_pointrange() +
  #scale_x_continuous(limits = c(-0.05, 0.18)) +
  geom_vline(xintercept=0, linetype="dashed", color = "darkgray") +
  labs(y = "Excluded District-Election", x = "Coefficient") +
  scale_color_manual(values=c("red4", "blue4"), name = "Term",
                     labels = c("Vote Share", expression(paste("Vote Share"^"2")))) +
  theme_bw() +
  theme(legend.position = "bottom")

#######################
## Panel B of Figure A4
#######################
continuous_linear_est = c(.0225256, .0219297, .0191672, .0192142,
                          .0215324, .0283813, .0292979, .0239155,
                          .0206952, .0285879, .0222623, .0221542,
                          .0139156,.0175511,.0209965,.0221945,
                          .0225391,.0218962,.0219351)
continuous_sq_est = c(-.0001963, -.0001894, -.0001821, -.0001541,
                      -.0001841, -.0002401, -.0002711, -.0002129,
                      -.0001852, -.000245, -.0001936, -.000192,
                      -.000124,-.0001399,-.0001806,-.0001932,
                      -.0001942,-.0001898,-.000175)

continuous_linear_se = c(.0076127, .007601, .0076302, .0074867,
                         .007563, .0086165, .007514, .0084309,
                         .0075855, .0079398, .0076142, .00761,
                         .0075318,.0081908,.0082322,.0077793,
                         .0075115,.0075966,.0079103)
continuous_sq_se = c(.0000706, .0000706,  .00007, .0000696,
                     .0000702, .0000772, .0000694, .0000813,
                     .00007, .0000725, .0000709, .0000706,
                     .0000691,.0000752,.0000745,.0000739,.0000697,
                     .0000702, .0000748)

continuous_linear_dta = data.frame(left_out, continuous_linear_est, continuous_linear_se)
continuous_linear_dta$term = "linear"
colnames(continuous_linear_dta) <- c("left_out", "est", "se", "term")
continuous_sq_dta = data.frame(left_out, continuous_sq_est, continuous_sq_se)
continuous_sq_dta$term = "squared"
colnames(continuous_sq_dta) <- c("left_out", "est", "se", "term")


continuous_dta = rbind(continuous_linear_dta, continuous_sq_dta)
continuous_dta$left_out = factor(continuous_dta$left_out, levels = c("2-2009", "4-2009", "6-2009",
                                                                     "1-2011", "3-2011", "5-2011", "7-2011",
                                                                     "2-2013", "4-2013", "6-2013",
                                                                     "1-2014",
                                                                     "1-2015", "3-2015", "5-2015", "7-2015",
                                                                     "2-2017", "4-2017", "6-2017",
                                                                     "5-2019"))

continuous_dta = continuous_dta %>%
  mutate(
    upper = est + 1.96 * se,
    lower = est - 1.96 * se)

## Plot
figureA4b = ggplot(continuous_dta, aes(x = as.numeric(est), 
                                                      y = as.factor(left_out), 
                                                      xmin=lower, 
                                                      xmax=upper,
                                                      color = factor(term), group = factor(term))) +
  geom_pointrange() +
  #scale_x_continuous(limits = c(-0.05, 0.15)) +
  geom_vline(xintercept=0, linetype="dashed", color = "darkgray") +
  labs(y = "Excluded District-Election", x = "Coefficient") +
  scale_color_manual(values=c("red4", "blue4"), name = "Term",
                     labels = c("Vote Share", expression(paste("Vote Share"^"2")))) +
  theme_bw() +
  theme(legend.position = "bottom")

